# encoding=utf8
# -*- coding: utf-8 -*
__author__ = 'mmfu.cn@gmail.com'

import numpy as np


def load_data(filename, sent_len):
    action_label_dic={}
    target_label_dic={}
    action_count=0
    target_count=0
    data=[]
    with open(filename,'r',encoding='utf8') as f:
        lines = f.readlines()
        for line in lines:
            lins = line.strip().split('\t')

#            if lins[0]=='拒识' or lins[0]=='集外说法':
#                continue

            if len(lins[-1].split(' '))>=sent_len:
                content=lins[2].split(' ')[:sent_len]
                pos=lins[-1].split(' ')[:sent_len]
            else:
                content=lins[2].split(' ')
                pad=['<PAD>']*(sent_len-len(lins[2].split(' ')))
                content.extend(pad)

                pos=lins[-1].split(' ')
                pos_pad=['<POS>']*(sent_len-len(lins[-1].split(' ')))
                pos.extend(pos_pad)

            data.append([lins[0],lins[1],content,pos])
            if lins[0] not in action_label_dic:
                action_label_dic[lins[0]]=action_count
                action_count+=1

            if lins[1] not in target_label_dic:
                target_label_dic[lins[1]]=target_count
                target_count+=1
    return data,action_label_dic, target_label_dic

def load_test_data(filename, sent_len):
    data=[]
    with open(filename,'r',encoding='utf8') as f:
        lines = f.readlines()
        for line in lines:
            lins = line.strip().split(' ')

            if len(lins)>=sent_len:
                content=lins[:sent_len]
            else:
                content=lins
                pad=['<PAD>']*(sent_len-len(lins))
                content.extend(pad)
            data.append(content)
    return data

def load_embed(embed_file,dim=300):
    embeds=[]
    word2id={}

    word2id['<PAD>'] = 0
    word2id['<UNK>'] = 1
    embeds.append(np.zeros(dim))
    embeds.append(np.zeros(dim))

    with open(embed_file, 'r', encoding='utf-8') as f:
        for line in f:
            l = line.strip().split()
            if len(l)<dim:
                print(l)
                continue
            word2id[l[0]] = len(embeds)
            embeds.append(np.array(l[1:]).astype(float))

    embeds = np.array(embeds, dtype='float32')
    return embeds, word2id

def load_pos(filename):
    pos2id={}
    pos2id['<POS>']=0
    i=1
    with open(filename,'r',encoding='utf8') as f:
        lines = f.readlines()
        for line in lines:
            lins = line.strip().split('\t')
            poses=lins[3].strip().split(' ')
            for pos in poses:
                if pos not in pos2id:
                    pos2id[pos]=i
                    i+=1
    return pos2id

def map2id(data, word2id, action2id, target2id, pos2id):
    actions=[]
    targets=[]
    querys=[]
    poses =[]
    oov_set=set()
    for da in data:
        actions.append(action2id[da[0]])
        targets.append(target2id[da[1]])
        # querys.append([word2id[word] if word in word2id else '<UNK>' for word in da[2]])
        q=[]
        for word in da[2]:
            if word not in word2id:
                oov_set.add(word)
                word='<UNK>'
            q.append(word2id[word])
        querys.append(q)

        p=[]
        for pos in da[3]:
            if pos not in pos2id:
                pos='<POS>'
            p.append(pos2id[pos])
        poses.append(p)

    print("The number of OOV is:"+str(len(oov_set)))
    return actions, targets, querys, poses

def map2id_test(data, word2id):

    querys=[]
    oov_set=set()
    for da in data:
        # querys.append([word2id[word] if word in word2id else '<UNK>' for word in da[2]])
        q=[]
        for word in da:
            if word not in word2id:
                oov_set.add(word)
                word='<UNK>'
            q.append(word2id[word])
        querys.append(q)
    print("The number of OOV test is:"+str(len(oov_set)))
    return querys
